/**
 * FriendMakeController.java
 */
package com.tobeface.sns.interfaces;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.kissme.core.orm.Page;
import com.tobeface.sns.application.pal.FriendService;
import com.tobeface.sns.domain.User;
import com.tobeface.sns.domain.UserNotExistsException;
import com.tobeface.sns.interfaces.util.UserUtil;

/**
 * @author Dylan
 * @time 2012-8-8
 */
@Controller
@RequestMapping(value = "/friend-make")
public class FriendMakeController {

	@Resource
	private FriendService friendService;

	@RequestMapping(value = "/make", method = RequestMethod.POST)
	public void makeFriend(User friend, BindingResult result) {
		if (result.hasErrors()) {
			throw new UserNotExistsException();
		}
		User self = UserUtil.getCurrentLoginUser();
		friendService.makeFriend(self, friend);
	}
	
	@RequestMapping(value = "/break", method = RequestMethod.DELETE)
	public void breakFriend(User friend, BindingResult result) {
		if (result.hasErrors()) {
			throw new UserNotExistsException();
		}
		User self = UserUtil.getCurrentLoginUser();
		friendService.breakFriend(self, friend);
	}

	@RequestMapping(value = "/{id}/find-id", method = RequestMethod.GET)
	public String find(@PathVariable("id") String id, Model model) {
		User user = friendService.findOne(id);
		model.addAttribute(user);
		return "";
	}

	@RequestMapping(value = "/{name}/find-name", method = RequestMethod.POST)
	public String findByName(Page<User> page, @PathVariable("name") String name, Model model) {
		page = friendService.findByName(page, name);
		model.addAttribute(page);
		return "";
	}

	@RequestMapping(value = "/find-address", method = RequestMethod.POST)
	public String findByAddress(Page<User> page, Model model) {
		User self = UserUtil.getCurrentLoginUser();
		page = friendService.findByAddress(page, self);
		model.addAttribute(page);
		return "";
	}
	
	@RequestMapping(value="/find-vocation",method=RequestMethod.POST)
	public String findByVocation(Page<User> page, Model model){
		User self = UserUtil.getCurrentLoginUser();
		page = friendService.findByVocation(page, self);
		model.addAttribute(page);
		return "";
	}
}
